package com.carepad.action.vital;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.carepad.action.AbstractAction;
import com.carepad.entity.Vital;
import com.carepad.service.VitalRecordService;

public class VitalDataViewAction extends AbstractAction {

	private static final long serialVersionUID = 1L;

	@Qualifier("vitalRecordService")
	@Autowired
	private VitalRecordService vitalRecordService;

	private Vital vitalRecord;
	private InputStream inputStream;

	public String execute() {
		String hcsId = getSessionPatient().getHcsID();
		Date d1 = getSessionImplementationDate().getEnDate();
		SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY");
		String executionDate = df.format(d1);
		try {
			vitalRecord = vitalRecordService.findByHcsIdandDate(hcsId,
					executionDate);
			String json = "";
			if (vitalRecord != null) {
				/*json = "({" + BLOOD_PRESSURE_HIGH + ":" + vitalRecord.getSbp()
						+ "," + BLOOD_PRESSURE_LOW + ":" + vitalRecord.getDbp()
						+ "," + PULSE_RATE + ":" + vitalRecord.getPr() + ","
						+ BODY_TEMPERATURE + ":" + vitalRecord.getBt() + "})";*/
				json = "{\"vital0\":" + vitalRecord.getSbp()
						+ ", \"vital1\":" + vitalRecord.getDbp()
						+ ", \"vital2\":" + vitalRecord.getPr()
						+ ", \"vital3\":" + vitalRecord.getBt() + "}";
			}
			inputStream = new ByteArrayInputStream(json.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public Vital getVitalRecord() {
		return vitalRecord;
	}

	public void setVitalRecord(Vital vitalRecord) {
		this.vitalRecord = vitalRecord;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

}
